<?php
/**
 * User: nickbai
 * Date: 2017/10/31 12:47
 * Email: 1902822973@qq.com
 */
namespace app\admin\controller;

use app\service\model\Chatlog;

class System extends Base
{
    // 自动回复设置
    public function reply()
    {
        if (request()->isPost()) {
            $param = input('post.');
            if (empty($param['word'])) {
                return json(['code' => -1, 'data' => '', 'msg' => '回复内容不能为空']);
            }

            try {
                db('reply')->where('id', 1)->update($param);
            } catch (\Exception $e) {
                return json(['code' => -2, 'data' => '', 'msg' => $e->getMessage()]);
            }

            return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
        }

        $info = db('reply')->where('id', 1)->find();
        $this->assign([
            'info' => $info,
            'status' => config('kf_status')
        ]);

        return $this->fetch();
    }

    // 客服设置
    public function customerService()
    {
        if (request()->isPost()) {
            $param = input('post.');
            db('config')->where('id', 1)->update($param);

            return json(['code' => 1, 'data' => '', 'msg' => '设置成功']);
        }

        $this->assign([
            'config' => db('config')->where('id', 1)->find(),
            'status' => config('kf_status')
        ]);

        return $this->fetch();
    }

    // 历史会话记录
    public function wordsLog()
    {
        if (request()->isAjax()) {
            $param = input('param.');
            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            // 默认显示最近7天
            $start = input('param.start');
            $end = input('param.end');

            $query = ChatLog::with('user');
            $countTmp = db('chat_log');
            if (!empty($param['searchText'])) {
                $query->where('from_name|to_name', $param['searchText']);
                $countTmp->where('from_name|to_name', $param['searchText']);
            }
            if (!empty($param['user_id'])) {
                $query->where('user_id', $param['user_id']);
                $countTmp->where('user_id', $param['user_id']);
            }

            if (!empty($start) && !empty($end) && $start <= $end) {
                $query->whereBetween('time_line', [strtotime($start), strtotime($end . ' 23:59:59')]);
                $countTmp->whereBetween('time_line', [strtotime($start), strtotime($end . ' 23:59:59')]);
            }

            $result = $query->limit($offset, $limit)->order('id', 'desc')->select()->map(function ($log) {
                $log->time_line = date('Y-m-d H:i:s', $log->time_line);
                $log->user_name = $log->user->name ?? '用户已注销';
                unset($log->user);
                return $log->toArray();
            });
            $return['total'] = $countTmp->count();  //总数据
            $return['rows'] = $result;
            return json($return);
        }
        $users = Chatlog::hasWhere('user')
            ->with('user')
            ->field('user_id')
            ->where('user_id', '>', 0)
            ->group('user_id')
            ->select()
            ->keyBy('user_id')
            ->map(
                fn ($log) => $log->user->name ?? $log->user->nickname
            );
        $this->assign([
            'users' => $users
        ]);
        return $this->fetch();
    }
}
